package com.eqdd.yiqidian.ui.gongneng.renshi.train;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows;
import com.eqdd.yiqidian.bean.rebuild.LectureListBean;
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 18-1-25  下午1:30.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class TrainLectureListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private int pageNum;
    private boolean isSelect;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "课程列表");
        initTopRightText("添加", view -> {
            startActivityForResult(AddTrainLectureActivity.class, null, Config.ADD_TRAIN_LECTURE);
        });
    }

    @SuppressWarnings("unchecked")
    @Override
    public void initData() {
        isSelect = getIntent().getBooleanExtra(Config.IS_SELECT, false);
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new DividerLineItemDecoration(this));
        SlimAdapterEx<LectureListBean> slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_105, new SlimInjector<LectureListBean>() {
            @Override
            public void onInject(LectureListBean data, IViewInjector injector) {
                String str = String.format(Locale.CHINA, "%s【%s】", data.getCourseTheme(), data.getCreateName());
                SpannableString span = new SpannableString(str);
                span.setSpan(new RelativeSizeSpan(0.8f), data.getCourseTheme().length(), span.length(),
                        SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
                injector.text(R.id.tv_title, span)
                        .clicked(R.id.tv_lecture, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Bundle bundle = new Bundle();
                                bundle.putInt(Config.ID, data.getId());
                                startActivity(TrainCourseMatchActivity.class);
                            }
                        });
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        if (isSelect) {
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        Intent intent = new Intent();
                        intent.putExtra(Config.BEAN, slimAdapterEx.getDataItem(position));
                        setResult(Config.SUCCESS, intent);
                        finish();
                    });
        }
        ModelRx2DataSource<LectureListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<HttpResultRows<LectureListBean>>post(HttpConfig.BASE_URL + HttpConfig.LECTURE_LIST)
                        .params("pageNumber", pageNum)
                        .params("userGuid", AppConstent.user.getGuid())
                        .converter(new JsonConverter<HttpResultRows<LectureListBean>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<>())
                        .flatMap(listResultBean -> {
                            pageNum = listResultBean.getItems().getPage();
                            if (listResultBean.getStatus() == 200) {
                                return Flowable.just(listResultBean.getItems() == null ? new ArrayList<LectureListBean>() : listResultBean.getItems().getRows());
                            } else {
                                ToastUtil.showShort(listResultBean.getMsg());
                                return Flowable.just(new ArrayList<>());
                            }
                        });
            }
        });
        slimAdapterEx.updateData(new ArrayList<>());
        MVCCoolHelper<List<LectureListBean>> mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();

    }

    @Override
    public void setView() {

    }
}
